﻿using System;
using System.Collections.Generic;
using System.Text;

using Gx.Dal.Employee;//数据
using Gx.Model.Entity;//实体
using Gx.Common;
using Gx.Model;
using Gx.Model.Enums;

namespace Gx.Bll.Employee
{
    public class E_EmployeeInfoBll
    {
        public ReturnModel<string> EmployeeLogin(string userLogin, string userPwd)
        {
            using (E_EmployeeInfoDal dal = new E_EmployeeInfoDal())
            {
                ReturnModel<string> r = new ReturnModel<string>();
                string md5Pwd = Base64Helper.Encryption(userPwd, ConfigHelper.CookieSecretKey);
                E_EmployeeInfo m = dal.EmployeeLogin(userLogin, md5Pwd);
                if (m == null)
                {
                    r.Code = (int)Code.操作失败;
                    r.Msg = "账户或密码不存在!";
                    return r;
                }
                CookieHelper.RemoveCookie(LoginHelper.UserCookie);
                List<string> key = new List<string>
                {
                    LoginHelper.GetCookieEncryption(GlobalHelper.COOKIEUSERID),
                    LoginHelper.GetCookieEncryption(GlobalHelper.COOKIEUSERNAME),
                    LoginHelper.GetCookieEncryption(GlobalHelper.COOKIEROLEID)
                };
                List<object> value = new List<object>
                {
                    LoginHelper.GetCookieEncryption(m.EmployeeId.ToString()),
                    LoginHelper.GetCookieEncryption(m.EmpName),
                    LoginHelper.GetCookieEncryption(m.EmployeeId.ToString())
                };
                CookieHelper.SetCookieValues(LoginHelper.UserCookie, key.ToArray(), value.ToArray(), DateTime.Now.AddHours(8));
                r.Code = (int)Code.操作成功;
                r.Msg = "登录成功!";
                r.Data = "/Home/Index";
                return r;
            }
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="passWord"></param>
        /// <returns></returns>
        public ReturnModel<bool> EditPassWord(string passWord)
        {
            using (E_EmployeeInfoDal dal = new E_EmployeeInfoDal())
            {
                ReturnModel<bool> r = new ReturnModel<bool>()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败",
                    Success = false,
                };
                if (string.IsNullOrEmpty(passWord))
                {
                    return r;
                }
                if (passWord.Length < 6)
                {
                    r.Msg = "密码长度不得低于5位";
                    return r;
                }
                string EmpPwd = Base64Helper.Encryption(passWord, ConfigHelper.CookieSecretKey);
                bool res = dal.RestPassWord(LoginHelper.GetUserId(), EmpPwd);
                if (res)
                {
                    r.Code = (int)Code.操作成功;
                    r.Msg = "修改密码成功!";
                    r.Success = true;
                    return r;
                }
                return r;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="empName"></param>
        /// <returns></returns>
        public List<E_EmployeeInfo> GetE_EmployeeInfoListLikeFront10(string empName)
        {
            if (empName == null)
            {
                empName = "";
            }
            using (E_EmployeeInfoDal dal = new E_EmployeeInfoDal())
            {
                List<E_EmployeeInfo> empBasicList = dal.GetE_EmployeeInfoListLikeFront10(empName);
                return empBasicList;
            }
        }

        public ReturnModel<bool> RestPassWord(int EmployeeId)
        {
            using (E_EmployeeInfoDal dal = new E_EmployeeInfoDal())
            {
                ReturnModel<bool> r = new ReturnModel<bool>()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败",
                    Success = false,
                };
                string EmpPwd = Base64Helper.Encryption("123456", ConfigHelper.CookieSecretKey);
                bool res = dal.RestPassWord(EmployeeId, EmpPwd);
                if (res)
                {
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功!";
                    r.Success = true;
                    return r;
                }
                return r;
            }
        }

        public List<E_EmployeeInfo> GetE_EmployeeInfoList(List<int> EmpIdList)
        {
            StringBuilder strWhere = new StringBuilder();
            strWhere.AppendFormat(" AND EmployeeId IN({0})", EmpIdList.Join());
            return GetE_EmployeeInfoList(strWhere.ToString());
        }

        public List<E_EmployeeInfo> GetE_EmployeeInfoList(string strWhere)
        {
            using (E_EmployeeInfoDal dal = new E_EmployeeInfoDal())
            {
                return dal.GetE_EmployeeInfoList(strWhere);
            }
        }
    }
}
